package com.huluobo.a2112mvvmdouyindemo.setting;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.huluobo.a2112mvvmdouyindemo.R;
import com.huluobo.a2112mvvmdouyindemo.databinding.ActivitySettingBinding;
import com.huluobo.a2112mvvmdouyindemo.login.LoginActivity;
import com.huluobo.a2112mvvmdouyindemo.utils.ImageLoader;
import com.huluobo.mvvm_base.bean.SettingBeanResult;
import com.huluobo.mvvm_base.bean.UserBeanResult;
import com.huluobo.mvvm_base.db.AppDBUtils;
import com.huluobo.mvvm_base.net.Const;
import com.huluobo.mvvm_base.view.BaseMVVMActivity;
import com.hyphenate.chat.EMClient;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

public class SettingActivity extends BaseMVVMActivity<ActivitySettingBinding, SettingViewModel> {
    private UserBeanResult.UserBean userBean;

    @Override
    protected void initData() {
        viewModel.updateIconSuccess.observe(this, settingBeanResult -> {
            userBean.setIcon(settingBeanResult.getData());//重新给数据库中的用户信息赋值
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
            ImageLoader.getInstance().loadCircleImage(this, settingBeanResult.getData(), dataBinding.settingUserIcon);

        });

        viewModel.updateIconFailed.observe(this, s -> {
            Log.i("TagA", "上传头像失败:" + s);
        });

        viewModel.getUserSuccess.observe(this, userBean -> {
            this.userBean = userBean;
            ImageLoader.getInstance().loadCircleImage(this, userBean.getIcon(), dataBinding.settingUserIcon);
            dataBinding.settingAddressEt.setText(userBean.getAddress());
            dataBinding.settingNicknameEt.setText(userBean.getNickname());
            dataBinding.settingBirthEt.setText(userBean.getBirth());
            dataBinding.settingSexEt.setText(userBean.getSex());
        });

        viewModel.updateAddressSuccess.observe(this, settingBeanResult -> {
            userBean.setAddress(settingBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
            dataBinding.settingAddressEt.setText(settingBeanResult.getData());
        });

        viewModel.updateAddressFailed.observe(this, s -> {
            Log.i("TagA", "更新地址失败:" + s);
        });

        viewModel.updateNicknameSuccess.observe(this, settingBeanResult -> {
            userBean.setNickname(settingBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
            dataBinding.settingNicknameEt.setText(settingBeanResult.getData());
        });

        viewModel.updateNicknameFailed.observe(this, s -> {
            Log.i("TagA", "更新昵称失败:" + s);
        });

        viewModel.updateBirthSuccess.observe(this, settingBeanResult -> {
            userBean.setBirth(settingBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
            dataBinding.settingBirthEt.setText(settingBeanResult.getData());
        });

        viewModel.updateBirthFailed.observe(this, s -> {
            Log.i("TagA", "更新生日失败:" + s);
        });

        viewModel.updateSexSuccess.observe(this, settingBeanResult -> {
            userBean.setSex(settingBeanResult.getData());
            AppDBUtils.getInstance().getUserDao().updateUserInfo(userBean);
            dataBinding.settingSexEt.setText(settingBeanResult.getData());
        });

        viewModel.updateSexFailed.observe(this, s -> {
            Log.i("TagA", "更新性别失败:" + s);
        });
    }

    @Override
    protected void initView() {
        viewModel.getUserByName(SPUtils.getInstance().getString(Const.SP_USERNAME));
        dataBinding.settingUserIcon.setOnClickListener(v -> {
            selectPic();
        });

        dataBinding.settingAddressBtn.setOnClickListener(v -> {
            viewModel.updateAddress(
                    dataBinding.settingAddressEt.getText().toString()
            );
        });

        dataBinding.settingNicknameBtn.setOnClickListener(v -> {
            viewModel.updateNickname(
                    dataBinding.settingNicknameEt.getText().toString()
            );
        });

        dataBinding.settingBirthBtn.setOnClickListener(v -> {
            viewModel.updateBirth(
                    dataBinding.settingBirthEt.getText().toString()
            );
        });

        dataBinding.settingSexBtn.setOnClickListener(v -> {
            viewModel.updateSex(
                    dataBinding.settingSexEt.getText().toString()
            );
        });

        dataBinding.settingLogout.setOnClickListener(v -> {
            AppDBUtils.getInstance().getUserDao().deleteUserInfo(userBean);
            EMClient.getInstance().logout(true);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            finish();
        });
    }

    private void selectPic() {
        PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            assert data != null;
            Bundle bundle = data.getExtras();
            //通过bundle拿到图片对象
            assert bundle != null;
            PictureBean bean = bundle.getParcelable(PictureSelector.PICTURE_RESULT);
            assert bean != null;
            String filePath = bean.getPath();
            viewModel.updateUserIcon(filePath);
        }
    }

    @Override
    protected Class<SettingViewModel> getViewModel() {
        return SettingViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }
}